package edu.bbu.BS.api;

import java.util.ArrayList;
import java.util.List;





import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;






import org.apache.log4j.Logger;

import edu.bbu.bs.backend.dtos.AuthorDTO;
import edu.bbu.bs.backend.model.Author;
import edu.bbu.bs.backend.service.AuthorService;
import edu.bbu.bs.backend.service.beans.AuthorServiceBean;
import edu.bbu.bs.backend.service.exceptions.BsServiceException;


@Stateless
@Local
@Path("authors")
@Consumes(MediaType.APPLICATION_JSON)
public class AuthorResource {

	@EJB
	private AuthorService authorService;

	@EJB
	private AuthorConverter authorConverter;
	
	private static Logger logger = Logger.getLogger(AuthorResource.class);

	@GET
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getAuthors() {
		logger.info("getAuthors()");		
		Response r;

		try {
			List<Author> authors = authorService.getAllAuthor();

			if ((authors != null) && (authors.size() > 0)) {
				List<AuthorDTO> authorDTOs = new ArrayList<AuthorDTO>(authors.size());

				for (Author author : authors) {
					authorDTOs.add(authorConverter.ModelToDTO(author));
				}

				GenericEntity<List<AuthorDTO>> authorsEntityList = new GenericEntity<List<AuthorDTO>>(authorDTOs) {
				};

				r = Response.ok(authorsEntityList).build();
			} else {
				r = Response.noContent().build();
			}
		} catch (BsServiceException ex) {
			r = Response.serverError().build();
		}

		return r;
	}

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{id}")
	public Response getAuthor(@PathParam("id") long id) {
		logger.info("getAuthors()");
		Response r;

		try {
			Author author = authorService.getAuthor(id);

			if (author != null) {
				System.out.println(author.getFirstName());
				r = Response.ok(authorConverter.ModelToDTO(author)).build();
			} else {
				System.out.println("null");
				r = Response.noContent().build();
			}
		} catch (BsServiceException ex) {
			r = Response.serverError().build();
		}

		return r;
	}

	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{id}")
	public Response modifyAuthor(AuthorDTO authorDTO) {		
		logger.info("modifyAuthor");
		Response r;

		try {
			Author author = authorConverter.DTOToModel(authorDTO);
			Author searchedAuthor = authorService.getAuthor(author.getId());

			if (searchedAuthor != null) {
				r = Response.ok(authorConverter.ModelToDTO(authorService.updateAuthor(author))).build();
			} else {
				throw new BsServiceException("The resource does not exist");
			}

		} catch (BsServiceException ex) {
			r = Response.serverError().build();
		}

		return r;
	}

	@POST
	@Produces(MediaType.APPLICATION_JSON)
	public Response insertAuthor(AuthorDTO authorDTO) {
		System.out.println("insertAuthor");
		logger.info("insertAuthor");
		Response r;

		try {
			Author insertedAuthor = authorService.insertAuthor(authorConverter.DTOToModel(authorDTO));

			r = Response.ok(authorConverter.ModelToDTO(insertedAuthor)).build();
		} catch (BsServiceException ex) {
			r = Response.serverError().build();
		}

		return r;
	}

	@DELETE
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{id}")
	public void deleteAuthor(@PathParam("id") long id) {
		System.out.println("deleteAuthor");
		logger.info("deleteAuthor");
//		Response r;

		try {
			authorService.deleteAuthor(authorConverter.DTOToModel(new AuthorDTO(id)));

//			r = Response.ok().build();
		} catch (BsServiceException ex) {
//			r = Response.serverError().build();
//			ex.printStackTrace();
		}

//		return r;
	}
}
